﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExplorerPlus.PluginsInterface;
using ExplorerPlus.DomainObjects;
using ExplorerPlus.AmazonApi;
using ExplorerPlus.Plugins.Common;

namespace ExplorerPlus.Plugins.Music
{
    public class MusicWebSearchPlugin : IWebSearchPlugin
    {
        #region IWebSearchPlugin Members

        public string[] GetSearchColumns()
        {
            return new string[] { "Title", "Artist", "Manufacturer", "ASIN" };
        }

        public List<string[]> Search(string keyword)
        {
            AmazonClient client = new AmazonApi.AmazonClient();
            AmazonApi.AmazonWebService.ItemSearchResponse response =
                client.Search("Music", keyword);

            List<string[]> albums = new List<string[]>();

            if (response.Items != null && response.Items.Length > 0 && response.Items[0].Item != null)
            {
                foreach (var item in response.Items[0].Item)
                {
                    if (item.ItemAttributes.Artist == null)
                    {
                        item.ItemAttributes.Artist = new string[] { "" };
                    }

                    if (item.ItemAttributes.Manufacturer == null)
                    {
                        item.ItemAttributes.Manufacturer = "";
                    }

                    albums.Add(
                        new string[] {
                        item.ItemAttributes.Title, 
                        item.ItemAttributes.Artist[0], 
                        item.ItemAttributes.Manufacturer, 
                        item.ASIN });
                }
            }

            return albums;
        }

        public List<string[]> SearchAdvanced(string keyword)
        {
            AmazonClient client = new AmazonApi.AmazonClient();
            AmazonApi.AmazonWebService.ItemSearchResponse response =
                client.SearchAdvanced("Music", keyword);

            List<string[]> albums = new List<string[]>();

            if (response.Items != null && response.Items.Length > 0 && response.Items[0].Item != null)
            {
                foreach (var item in response.Items[0].Item)
                {
                    if (item.ItemAttributes.Artist == null)
                    {
                        item.ItemAttributes.Artist = new string[] { "" };
                    }

                    if (item.ItemAttributes.Manufacturer == null)
                    {
                        item.ItemAttributes.Manufacturer = "";
                    }

                    if (item.SmallImage == null)
                    {
                        item.SmallImage = new AmazonApi.AmazonWebService.Image();
                    }

                    albums.Add(
                        new string[] {
                        item.ItemAttributes.Title, 
                        item.ItemAttributes.Artist[0], 
                        item.ItemAttributes.Manufacturer, 
                        item.ASIN, item.SmallImage.URL });
                }
            }

            return albums;
        }

        public Item FindItem(string[] keywords)
        {
            AmazonClient client = new AmazonApi.AmazonClient();
            AmazonApi.AmazonWebService.ItemLookupResponse response =
                client.FindItemByASIN(keywords[3]);
            if (response.Items.Length == 0 || response.Items[0].Item == null)
            {
                return null;
            }
            AmazonApi.AmazonWebService.Item item = response.Items[0].Item[0];

            Item album = new Item((int)Categories.Music);
            album.ItemName = item.ItemAttributes.Title;
            album.AddItemProperty((int)MusicFields.DetailPageUrl, item.DetailPageURL);
            album.AddItemProperty((int)MusicFields.Manufacturer, item.ItemAttributes.Manufacturer);
            album.AddItemProperty((int)MusicFields.Binding, item.ItemAttributes.Binding);
            album.AddItemProperty((int)MusicFields.NoOfDiscs, item.ItemAttributes.NumberOfDiscs);
            album.AddItemProperty((int)MusicFields.Edition, item.ItemAttributes.Edition);
            if (item.ItemAttributes.OriginalReleaseDate != null)
            {
                album.AddItemProperty((int)MusicFields.ReleaseDate, item.ItemAttributes.OriginalReleaseDate);
            }
            else
            {
                album.AddItemProperty((int)MusicFields.ReleaseDate, item.ItemAttributes.ReleaseDate);
            }
            album.AddItemProperty((int)MusicFields.SalesRank, item.SalesRank);
            if (item.ItemAttributes.Artist != null)
            {
                foreach (string artist in item.ItemAttributes.Artist)
                {
                    album.AddItemProperty((int)MusicFields.Artist, artist);
                }
            }
            if (item.LargeImage != null)
            {
                album.AddItemProperty((int)MusicFields.LargeImage, item.LargeImage.URL);
            }
            if (item.ItemAttributes.ListPrice != null)
            {
                album.AddItemProperty((int)MusicFields.ListPrice, item.ItemAttributes.ListPrice.FormattedPrice);
            }
            if (item.SmallImage != null)
            {
                album.AddItemProperty((int)MusicFields.SmallImage, item.SmallImage.URL);
            }
            if (item.CustomerReviews != null)
            {
                album.AddItemProperty((int)MusicFields.Rating, item.CustomerReviews.AverageRating.ToString());
                album.AddItemProperty((int)MusicFields.TotalCustomerReviews, item.CustomerReviews.TotalReviews);
            }
            if (item.EditorialReviews != null && item.EditorialReviews.Length > 0)
            {
                for (int i = 0; i < item.EditorialReviews.Length; i++)
                {
                    album.AddItemProperty((int)MusicFields.EditorialReviews,
                        string.Format("<b>{0}</b><br>{1}",
                        item.EditorialReviews[i].Source,
                        item.EditorialReviews[i].Content));
                }
            }

            if (item.Tracks != null && item.Tracks.Length > 0)
            {
                foreach (var disc in item.Tracks)
                {
                    for (int i = 0; i < disc.Track.Length; i++)
                    {
                        var track = disc.Track[i];
                        album.AddItemProperty((int)MusicFields.Track, disc.Number + "#" + track.Number + "#" + track.Value);
                    }
                }
            }

            List<string> catArr = PluginHelper.GetBrowseNodesParent(item.BrowseNodes, "Styles");
            if (!catArr.Contains("All"))
            {
                catArr.Add("All");
            }
            foreach (string cat in catArr)
            {
                album.AddItemProperty((int)MusicFields.Style, cat);
            }

            return album;
        }

        public void DownloadItemImages(Item item, string imagesPath)
        {
            // Add logic to download book images to images folder
            PluginHelper.SaveImageToDisk(item, (int)MusicFields.LargeImage, imagesPath);
            PluginHelper.SaveImageToDisk(item, (int)MusicFields.SmallImage, imagesPath);
        }

        #endregion
    }
}
